/**
 * Created with IntelliJ IDEA.
 * User: guangyu
 * Date: 3/12/13
 * Time: 9:49 AM
 * To change this template use File | Settings | File Templates.
 */
/**
 *
 * @class
 * @extends ge.Class
 */
ge.Queue = ge.Class.extend(/**@lends ge.Queue#*/
    {

        _data:new Array(),
        size:function () {
            return this._data.length
        },
        add:function (item) {
            this._data.push(item);
        },
        remove:function () {
            return this._data.shift();
        },
        forEach:function (prop) {
            this._data.forEach(prop);
        },
        indexOf:function (item) {
            this._data.indexOf(item)
        },
        contains:function (item) {
            return this.indexOf(item) != -1;
        },
        clear:function (item) {
            this._data = null;
            this.data = new Array();
        },
        lastIndexOf:function (item) {
            return this.lastIndexOf(item);
        }
    }
);
/**
 *
 * @class
 * @extends ge.Class
 */
ge.Stack = ge.Class.extend(/**@lends ge.Stack#*/
    {

        _data:new Array(),
        size:function () {
            return this._data.length
        },
        push:function (item) {
            this._data.push(item);
        },
        pop:function () {
            return this._data.pop();
        },
        forEach:function (prop) {
            this._data.forEach(prop);
        },
        indexOf:function (item) {
            this._data.indexOf(item)
        },
        contains:function (item) {
            return this.indexOf(item) != -1;
        },
        clear:function (item) {
            this._data = [];
        },
        lastIndexOf:function (item) {
            return this.lastIndexOf(item);
        }
    }
);
/**
 *
 * @class
 * @extends ge.Class
 */
ge.Map = ge.Class.extend(/**@lends ge.Map#*/{
    keys:new Array(),
    data:new Object(),
    /**
     * 当前Map当前长度
     */
    size:function () {
        return this.keys.length;
    },

    /**
     * 添加值
     * @param {Object} key
     * @param {Object} value
     */
    put:function (key, value) {
        if (!this.containsKey(key)) {
            this.keys.push(key);
        }
        this.data[key] = value;

    },
    /**
     * 根据当前key获取value
     * @param {Object} key
     */
    get:function (key) {
        return this.data[key];
    },
    /**
     * 根据当前key移除Map对应值
     * @param {Object} key
     */
    remove:function (key) {
        var index = this.indexOf(key);
        if (index != -1) {
            this.keys.splice(index, 1);
        }
        this.data[key] = null;
    },
    /**
     * 清空Map
     */
    clear:function () {
        for (var i = 0, len = this.size(); i < len; i++) {
            var key = this.keys[i];
            this.data[key] = null;
        }
        this.keys.length = 0;
    },
    /**
     * 当前key是否存在
     * @param {Object} key
     */
    containsKey:function (key) {
        return this.data[key] != null;
    },
    /**
     * 是否为空
     */
    isEmpty:function () {
        return this.keys.length === 0;
    },
    /**
     * 类型Java中Map.entrySet
     */
    entrySet:function () {
        var size = this.size();
        var datas = new Array(size);
        for (var i = 0, len = size; i < len; i++) {
            var key = this.keys[i];
            var value = this.data[key];
            datas[i] = {
                'key':key,
                'value':value
            }
        }
        return datas;
    },
    /**
     * 遍历当前Map
     * var map = new Map();
     * map.put('key', 'value');
     * map.each(function(index, key, value){
     *         console.log("index:" + index + "--key:" + key + "--value:" + value)
     * })
     * @param {Object} fn
     */
    each:function (fn) {
        if (toString.call(fn) === '[object Function]') {
            for (var i = 0, len = this.size(); i < len; i++) {
                var key = this.keys[i];
                fn(i, key, this.data[key]);
            }
        }
        return null;
    },
    /**
     * 获取Map中 当前key 索引值
     * @param {Object} key
     */
    indexOf:function (key) {
        var size = this.size();
        if (size > 0) {
            for (var i = 0, len = size; i < len; i++) {
                if (this.keys[i] == key)
                    return i;
            }
        }
        return -1;
    },
    /**
     * Override toString
     */
    toString:function () {
        var str = "{";
        for (var i = 0, len = this.size(); i < len; i++, str += ",") {
            var key = this.keys[i];
            var value = this.data[key];
            str += key + "=" + value;
        }
        str = str.substring(0, str.length - 1);
        str += "}";
        return str;
    },
    /**
     * 获取Map中的所有value值(Array)
     */
    values:function () {
        var size = this.size();
        var values = new Array();
        for (var i = 0; i < size; i++) {
            var key = this.keys[i];
            values.push(this.data[key]);
        }
        return values;
    }

})
